//
//  TCC_Array.swift
//  Pods-TCCKit
//
//  Created by tucici on 2024/6/3.
//

import UIKit
#if canImport(TCC_View)
import TCC_View
#endif
@objc public enum SupplementaryViewOfKind : Int{
    case header = 0
    case footer = 1
}
@objc public extension UICollectionView {
    
    /// 以class注册cell
    /// - Parameters:
    ///   - cellClass: UICollectionViewCell
    ///   - id: UICollectionViewCell的id
    func register(_ cellClass: AnyClass, _ id : String?=nil){
        self.register(cellClass, forCellWithReuseIdentifier: getID(cellClass, id))
    }
    
    /// 以nib注册cell
    /// - Parameters:
    ///   - nib: UICollectionViewCell
    ///   - id: UICollectionViewCell的 ID
    func register(nib: AnyClass, _ id : String?=nil){
        self.register( nib.nib(), forCellWithReuseIdentifier: getID(nib, id))
    }
    
    /// 以class 注册UICollectionReusableView
    /// - Parameters:
    ///   - Class: UICollectionReusableView
    ///   - kind: 注册是header还是footer
    ///   - id: UICollectionReusableView的ID
    func register(_ Class: AnyClass,  _ kind: SupplementaryViewOfKind, _ id : String?=nil){
        let str = kind == .header ? UICollectionView.elementKindSectionHeader :  UICollectionView.elementKindSectionFooter
        self.register(Class, forSupplementaryViewOfKind:str,withReuseIdentifier: getID(Class, id))
    }
    
    /// 以nib注册UICollectionReusableView
    /// - Parameters:
    ///   - nib: UICollectionReusableView的nib
    ///   - kind: 注册是header还是footer
    ///   - id: UICollectionReusableView的ID
    func register(nib: AnyClass, _ kind: SupplementaryViewOfKind, _ id : String?=nil){
        let str = kind == .header ? UICollectionView.elementKindSectionHeader :  UICollectionView.elementKindSectionFooter
        self.register(nib.nib(), forSupplementaryViewOfKind:str,withReuseIdentifier: getID(nib, id))
    }
    
    
    /// dequeueReusableCell
    /// - Parameters:
    ///   - Class: UICollectionViewCell
    ///   - indexpath: IndexPath
    /// - Returns: UICollectionViewCell
    @nonobjc func cell<T:UICollectionViewCell>(_ Class:AnyClass, _ indexpath:IndexPath)->T{
        let cell = self.dequeueReusableCell(withReuseIdentifier:Class.ID(), for:indexpath)
        return cell as! T
    }
    
    
    /// dequeueReusableView
    /// - Parameters:
    ///   - Class: UICollectionReusableView
    ///   - kind: 是header还是footer
    ///   - indexpath: indexpath
    /// - Returns: UICollectionReusableView
    @nonobjc func reusableView<T:UICollectionReusableView>(_ Class:AnyClass,_ kind:SupplementaryViewOfKind , _ indexpath:IndexPath)->T{
        let str = kind == .header ? UICollectionView.elementKindSectionHeader :  UICollectionView.elementKindSectionFooter
        let view = self.dequeueReusableSupplementaryView(ofKind: str, withReuseIdentifier: Class.ID(), for: indexpath)
        return view as! T
    }
    
    private func getID(_ Class: AnyClass,_ id:String?)->String{
        return id == nil ? Class.ID() : id!
    }
}
